<?php

namespace app\index\controller;

use think\Exception;
use think\Db;

class BuyMyClass extends IndexController
{
    /**
     * 显示购买页.
     *
     * @return \think\Response
     */
    public function payfor($id)
    {
        // 查询数据
        //带着id去找该课程的信息
        $course = Db::name('course')->alias('c')
            ->field('c.name,c.id,c.description,c.price,p.address')
            ->join('coursepicture p','p.courseid=c.id')
            ->where('c.id',$id)
            ->where('c.display','1')
            ->where('p.cover', '1')
            ->buildsql();
        $res = Db::query($course);

        if( empty($res) ){
            $this->error('暂无数据');
        }

        $data['price'] = $res[0]['price'];


        return view('class/pay', [
            'data' => $res,
            'price' => $data['price']
        ]);
    }

    /**
     * 执行购买
     */
    public function doPay ($id)
    {
        // 判断付款状态

        // 查询购买课程信息
        $course = Db::name('course')->alias('c')
            ->field('c.name,c.id,c.description,c.price,p.address')
            ->join('coursepicture p','p.courseid=c.id')
            ->where('c.id',$id)
            ->where('c.display','1')
            ->where('p.cover', '1')
            ->buildsql();
        $res = Db::query($course);

        if( empty($res) ){
            $this->error('暂无数据');
        }

        // 生成订单数据
        $data = [];
        $data['uid'] = session('web')['uid'];
        $data['cid'] = $res['0']['id'];
        $data['cname'] = $res['0']['name'];
        $data['face'] = $res['0']['address'];
        $data['createtime'] = time();
        $data['price'] = $res['0']['price'];


        // 付款成功后, 执行数据插入
        Db::name('myclass')
             ->insert($data);

        return view('class/success', [
            'data' => $data

        ]);
    }


    /**
     * 加入收藏
     * @param $id
     */
    public function iLike ($id)
    {


        // 查询购买课程信息
        $course = Db::name('course')->alias('c')
            ->field('c.name,c.description,c.price,p.address')
            ->join('coursepicture p','p.courseid=c.id')
            ->where('c.id',$id)
            ->where('c.display','1')
            ->where('p.cover', '1')
            ->buildsql();
        $res = Db::query($course);

        if( empty($res) ){
            $this->error('暂无数据');
        }

        // 生成收藏数据
        $data = [];
        $data['uid'] = session('web')['uid'];
        $data['categoryid'] = $id;
        $data['name'] = $res['0']['name'];
        $data['face'] = $res['0']['address'];
        $data['price'] = $res['0']['price'];
        $data['createtime'] = time();
        $data['description'] = $res['0']['description'];


        // 开启事务
        Db::startTrans();

        try{
            $res = Db::name('ilike')
                ->insert($data);

            if( $res>0 ){
                Db::commit();
                $this->success('已经加入收藏!');
            }
        }catch(Exception $exception){
            Db::rollback();
            $this->error('因未知错误, 添加收藏失败, 请重试...');
        }

    }


    /**
     * 我的收藏页
     * @return \think\response\View
     */
    public function myLikes ()
    {
        $uid = session('web')['uid'];

        $data = Db::name('ilike')
            ->field('categoryid, name, face, price, createtime, description')
            ->where('uid', $uid)
            ->select();

        return view('class/ilike', [
            'data' => $data
        ]);

    }

    public function iDis ($id)
    {
        $uid = session('web')['uid'];

        // 删除收藏表
        // 开启事务
        Db::startTrans();
        try{
            $res = Db::name('ilike')
                ->where('uid', $uid)
                ->where('categoryid', $id)
                ->delete();

            if($res>0){ Db::commit(); $this->success('已取消收藏');}
        }catch(Exception $exception){
            Db::rollback();
            $this->error('网路遭到不明怪兽攻击, 暂时无法取消, 请重试...');
        }

    }

}
